package com.qfedu.yanglao20admin.plancase.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.enuminfo.StateEnum;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.plancase.AccidentType;
import com.qfedu.yanglao20admin.plancase.dao.AccidentTypeDao;
import com.qfedu.yanglao20admin.plancase.param.AccidentTypeParam;
import com.qfedu.yanglao20admin.plancase.service.AccidentTypeService;
import com.qfedu.yanglao20admin.plancase.vo.AccidentTypeVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2023/4/21 19:57
 */
@Service
public class AccidentTypeServiceImpl implements AccidentTypeService {

    @Resource
    private AccidentTypeDao accidentTypeDao;

    @Override
    public Page<AccidentTypeVO> pageAccidentType(AccidentTypeParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<AccidentTypeVO> page = new Page<>(param.getPageNum(), param.getPageSize());

        List<AccidentTypeVO> accidentTypes = accidentTypeDao.selectList(param);

        //  循环遍历
        for (AccidentTypeVO vo :
                accidentTypes) {
            StateEnum stateById = StateEnum.getStateById(vo.getEnableFlag());
            vo.setStateDesc(stateById.getDesc());
        }

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = accidentTypeDao.selectCount(null);
        page.setTotal(aLong);

        page.setRecords(accidentTypes);

        return page;
    }

    @Override
    public void deleteById(Integer id) {
        accidentTypeDao.deleteById(id);
    }

    @Override
    public AccidentType queryAccidentType(Integer id) {
        return accidentTypeDao.selectById(id);
    }

    @Override
    public void insertAccidentType(AccidentType accidentType) {

        if (accidentType.getAccType() == null || accidentType.getAccType().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写事故类型");
        }

        AccidentType byAccType = accidentTypeDao.selectByAccType(accidentType.getAccType());
        if (byAccType != null) {
            throw new CommonException(CommonConstants.ERROR, "该事故类型已存在");
        }

        accidentTypeDao.insert(accidentType);
    }

    @Override
    public void updateAccidentType(AccidentType accidentType) {

        if (accidentType.getAccType() == null || accidentType.getAccType().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写事故类型");
        }

        AccidentType byAccType = accidentTypeDao.selectByAccType(accidentType.getAccType());
        if (byAccType != null) {
            if (!byAccType.getId().equals(accidentType.getId())) {
                throw new CommonException(CommonConstants.ERROR, "该事故类型已存在");
            }
        }
        accidentTypeDao.updateById(accidentType);
    }

    @Override
    public List<AccidentType> selectAll() {
        return accidentTypeDao.selectAll();
    }
}
